package com.bsoft.gol.hbs.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import pcn.bean.consultation.HiSysMsgTpl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @ProjectName: common-clinical
 * @Package: utils
 * @ClassName: BeanNotNullUtil
 * @Author: Jerash
 * @Description: Bean为不为空工具类
 * @Date: 2021/12/3 18:09
 * @Version: 1.0
 */

public class BeanNotNullUtil {
    private static Logger log = LoggerFactory.getLogger(BeanNotNullUtil.class);

    public static void main(String[] args) throws ParseException {
        HiSysMsgTpl req = new HiSysMsgTpl();
        req.setCd("1");
        HashMap<String, String> b = isNotEmptyBean(req, Arrays.asList("id","tplKey"));
        System.out.println(b);
        HashMap<String, String> c = isMustBeanName(req, Arrays.asList("tplKey"));
        System.out.println(c);
        Date  date=new Date();
        String objectId=dateToObjectId(date);
        System.out.println(objectId);

    }

    private static String dateToObjectId(Date date ) throws ParseException {
        StringBuffer objectId = new StringBuffer("");
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //转换为
        objectId.append(Long.toHexString((date.getTime()/1000)));
        //校验 objectId 的长度24位，不足24补0
        while (objectId.length()<24){
            objectId.append("0");
        }
        return objectId.toString();
    };


    /**
     * 判断对象内不允许为空的属性
     *
     * @param obj
     * @return
     */
    public static HashMap<String, String> isMustBeanName(Object obj, List<String> names) {
        String flag = "false";
        HashMap<String, String> res = new HashMap<>();
        try {
            if (null != obj) {
                //得到类对象
                Class<?> c = (Class<?>) obj.getClass();
                //得到属性集合
                Field[] fs = c.getDeclaredFields();
                //得到方法体集合
                Method[] methods = c.getDeclaredMethods();
                //遍历属性
                for (Field f : fs) {
                    //设置属性是可以访问的(私有的也可以)
                    f.setAccessible(true);
                    String fieldGetName = f.getName();
                    if (names != null && names.size() > 0) {
                        boolean isExits = names.contains(fieldGetName);
                        if (isExits) {
                            //得到此属性的值
                            Object val = f.get(obj);
                            //只要有1个属性不为空,那么就不是所有的属性值都为空
                            if (StringUtils.isEmpty(val)) {
                                flag = "true";
                                res.put("param", "参数[" + f.getName() + "]不能为空");
                                break;
                            }
                        }
                    } else {
                        break;
                    }

                }
            }
        } catch (Exception e) {
            log.error("判断对象是否为空出错：" + e.getMessage());
        }
        res.put("flag", flag);
        return res;
    }

    /**
     * 判断对象是否为空
     *
     * @param obj
     * @return
     */
    public static HashMap<String, String> isNotEmptyBean(Object obj, List<String> names) {
        String flag = "false";
        HashMap<String, String> res = new HashMap<>();
        try {
            if (null != obj) {
                //得到类对象
                Class<?> c = (Class<?>) obj.getClass();
                //得到属性集合
                Field[] fs = c.getDeclaredFields();
                //得到方法体集合
                Method[] methods = c.getDeclaredMethods();
                //遍历属性
                for (Field f : fs) {
                    //设置属性是可以访问的(私有的也可以)
                    f.setAccessible(true);
                    String fieldGetName = parGetName(f.getName());
                    //判断属性是否存在get方法
                    if (!checkGetMet(methods, fieldGetName)) {
                        continue;
                    }
                    //得到此属性的值
                    Object val = f.get(obj);
                    //只要有1个属性不为空,那么就不是所有的属性值都为空
                    if (StringUtils.isEmpty(val)) {
                        if (names != null) {
                            boolean isNull = names.contains(f.getName());
                            if (isNull) continue;
                        }
                        flag = "true";
                        res.put("param", "参数[" + f.getName() + "]不能为空");
                        break;
                    }
                }
            }
        } catch (Exception e) {
            log.error("判断对象是否为空出错：" + e.getMessage());
        }
        res.put("flag", flag);
        return res;
    }

    /**
     * 拼接某属性的 get方法
     *
     * @param fieldName
     * @return String
     */
    public static String parGetName(String fieldName) {
        if (null == fieldName || "".equals(fieldName)) {
            return null;
        }
        int startIndex = 0;
        if (fieldName.charAt(0) == '_')
            startIndex = 1;
        return "get"
                + fieldName.substring(startIndex, startIndex + 1).toUpperCase()
                + fieldName.substring(startIndex + 1);
    }

    /**
     * 判断是否存在某属性的 get方法
     *
     * @param methods
     * @param fieldGetMet
     * @return boolean
     */
    public static Boolean checkGetMet(Method[] methods, String fieldGetMet) {

        for (Method met : methods) {
            if (fieldGetMet.equals(met.getName())) {
                return true;
            }
        }
        return false;
    }

}
